<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Welcome extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

	public function index() {

        $this->oauth->oauthcheck();

        $userinfo = $_SESSION['userinfo'];
        $data['userinfo']=$userinfo;
        //$tokenvalue
        $this->load->view('header');
        $this->load->view('index2',$data);

	}

    public function chatroom(){

        $this->load->view('header');
        $this->load->view('chatroom');
    }

    /**
     * 排名页面
     * @param string 省份
     */
    public function rank($province='') {

         $cond = array(1=>1);
        if(!empty($province)) {
            $cond['u.province']=urldecode($province);
        }
        $this->db->select('u.realname,u.headimgurl,owner_openid,sum(fee) total_fee');
        $this->db->join('t_user u','u.openid=p.owner_openid');
        $this->db->group_by('owner_openid');
        $result = $this->db->get_where('t_payrecord p',$cond);
        $result = $result->result_array();
        //echo $this->db->last_query();
        //var_dump($result);

        $data['list']= $result;
        $this->load->view('header');
        $this->load->view('rank',$data);

    }

    public function goods($goodsid) {

        $openid = $_SESSION['openid'];

        if (empty($openid)) {
            redirect($this->getWechat()->wxOauthBase(base_url('/').'?redirect='.$_SERVER['REQUEST_URI']));
        }

        //查询已经发起的order,避免重复发起.
        $takendorder = $this->db->select('goodsid')->get_where('t_order', array('owner_openid'=>$openid));
        $takendorder = $takendorder->result_array();

        $historyorder = array();
        foreach ($takendorder as $item) {
            $historyorder[] = $item['goodsid'];
        }

        unset($takendorder);
        $data['historyorder']=$historyorder;
        $row = $this->db->get_where('t_goods',array('id'=>$goodsid));
        $row = $row->result_array();
        if(sizeof($row)>0) {
            $row = $row[0];
            $data['detail'] = $row;
            $this->load->view('header');
            $this->load->view('goods_detail',$data);
        }
    }


    public function start() {
        $openid = $_SESSION['openid'];

        if (empty($openid)) {
            redirect($this->getWechat()->wxOauthBase(base_url('/').'?redirect='.$_SERVER['REQUEST_URI']));
        }
        $firstorder = $this->db->get_where('t_order', array('owner_openid'=>$openid))->result_array()[0];
        if($firstorder) {
            redirect('./pay/showmemoney?orderid='.$firstorder['id']);
            //redirect('./user/createaccount');
        } else {
            redirect('./user/createaccount');
        }
    }

    public function visitprobe() {
        $now = date('YmdH',time());
        $this->db->query("insert into t_stat(id,visitcnt,ctimehour) values (null,1,$now)  on duplicate key update visitcnt=visitcnt+1");
    }
}
